package org.sky.util;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.sky.base.PageIndex;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class WebTool {
	
	public static String FLAGOFCURRENTTIME = "CURRENT";
	

	
	// 保留小数位,dout为输入实数,n为保留的位数
	public static double keepSmallDigital(double dout, int n) {
		double p = Math.pow(10, n);
		return Math.round(dout * p) / p;
		
	}
	
	//对字符串的简单检查
	public static boolean checkStatus(String param) {
		return null != param && !"".equals(param.trim());
	}
	
	public static boolean isDigit(String str){
		return str.replaceAll("\\d", "").equals("");
	}
	
	//对字符串的简单检查
	public static double stringToDouble(String s) {
		if ("".equals(s.trim()))
			return 0.0d;
		else
			return Double.parseDouble(s.trim());
	}
	
	public static int getOrdinalOfMonth(int month) {
		if(month<1 || month>12) return 0;
		return (month + 4) % 6 == 0 ? 6 : (month + 4) % 6;
	}
	
	/**
	 * 利用服务器的时间,计算当前时间对应的学年及其学期
	 * 
	 * @return results[0]代表的当前学年,results[1]代表的当前学期,,results[2]代表的当前月份
	 */
	public static List<String> getYearAndTerm() {
		Calendar calendar = Calendar.getInstance();
		List<String> results = new ArrayList<String>();
		int yearTemp = calendar.get(Calendar.YEAR);
		int monthTemp = calendar.get(Calendar.MONTH) + 1;
		if (monthTemp >= 3 && monthTemp <= 8) {
			results.add((yearTemp - 1) + "-" + yearTemp);
			results.add("二");
			results.add(monthTemp + "");
		} else {
			results.add(yearTemp + "-" + (yearTemp + 1));
			results.add("一");
			results.add(monthTemp + "");
		}
		return results;
	}


	/**
	 * 下载时调用的返回设置方法
	 * 
	 * @param fileName 文件的名字
	 * @param contentType 文件类型
	 * @param ext 文件类型的后缀
	 */
	public static void downloadFile(String fileName, String contentType,
			String ext) {
		Calendar cal = Calendar.getInstance();
		int y = cal.get(Calendar.YEAR);
		int m = cal.get(Calendar.MONTH) + 1;
		int d = cal.get(Calendar.DATE);
		int h = cal.get(Calendar.HOUR_OF_DAY);
		int min = cal.get(Calendar.MINUTE);
		int s = cal.get(Calendar.SECOND);
		ActionContext context = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) context
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType(contentType);
		fileName = y + "-" + m + "-" + d + "-" + h + "-" + min + "-" + s
				+ fileName;
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + "." + ext);
	}

    /**
     * 页面分页功能的工具
     */
    public static PageIndex getPageIndex(long viewPageCount, int currentPage, long totalPage) {
        long startPage = currentPage - (viewPageCount % 2 == 0 ? viewPageCount / 2 - 1
                : viewPageCount / 2);
        long endPage = currentPage + viewPageCount / 2;
        if (startPage < 1) {
            startPage = 1;
            if(totalPage>=viewPageCount) endPage = viewPageCount;
            else endPage = totalPage;
        }
        if(endPage>totalPage){
            endPage = totalPage;
            if((endPage-viewPageCount)>0) startPage = endPage-viewPageCount+1;
            else startPage = 1;
        }
        return new PageIndex(startPage,endPage);
    }

	
}
